<?php

/**
 * Created by PhpStorm.
 * User: thorjiang
 * Date: 2018/5/9
 * Time: 21:54
 */
namespace App\Models;

use DB;
use Illuminate\Database\Eloquent\Model;

/**
 * 通用模型
 * Class CommonModel
 * @package App\Models
 */
class CommonModel extends Model
{
    protected $primaryKey = 'id';
    protected $fillable = [];
    protected $searchable = [];
    protected $encrypt_fields = [];

    /**
     * @return mixed
     */
    public function getTable($uid = null)
    {
        return $this->table;
    }

    /**
     * @param $params
     * @return array
     */
    public function createRecord($params) {
        $ret = self::create($params);
        return empty($ret)?[]:$ret->toArray();
    }

    /**
     * @param $id
     * @return mixed
     */
    public function getById($id) {
        return self::where($this->primaryKey, $id)->first();
    }

    /**
     * @param $id
     * @param $params
     * @return mixed
     */
    public function updateRecord($id,$params)
    {
        $changeable = empty($this->changeable)?$this->fillable:$this->changeable;
        foreach ($params as $key=>$value){
            if(!in_array($key, $changeable) || is_null($value) || $value === ""){
                unset($params[$key]);
                continue;
            }
        }
        $ret =  self::where($this->primaryKey,$id)->update($params);

        return $ret;
    }

    /**
     * 获取单条
     * @param $params
     * @param string $filed
     * @return array
     */
    public function fetch($params, $filed = '*')
    {
        $query = self::select(DB::raw($filed));
        foreach ($params as $key=>$value){
            if(in_array($key,$this->searchable)){
                if (is_array($value) &&!empty($value)) {
                    $query->whereIn($key,$value);
                } elseif ($value!=='' && !is_null($value)) {
                    $query->where($key,$value);
                }
            }
        }
        $data = $query->first();

        return empty($data) ? [] : $data->toArray();
    }

    /**
     * 获取多条
     * @param $params
     * @param string $filed
     * @return array
     */
    public function fetchList($params, $filed = '*')
    {
        $query = self::select(DB::raw($filed));
        foreach ($params as $key=>$value){
            if(in_array($key,$this->searchable)){
                if (is_array($value) &&!empty($value)) {
                    $query->whereIn($key,$value);
                } elseif ($value!=='' && !is_null($value)) {
                    $query->where($key,$value);
                }
            }
        }
        $data = $query->get();

        return empty($data) ? [] : $data->toArray();
    }

    /**
     * 分页排序
     * @param $query
     * @param $params
     * @return array
     */
    public static function setPage($query, $params)
    {
        $ret = [];
        if (isset($params['sortMap'])) {
            foreach ($params['sortMap'] as $k => $v) {
                $query->orderBy($k, $v);
            }
        } elseif (isset($params['sort_key'])) {
            !isset($params['sort']) && $params['sort'] = 'asc';
            $query->orderBy(DB::raw($params['sort_key']), $params['sort']);
        }

        if (isset($params['page']) && empty($params['all'])) {
            $size = empty($params['size']) ? 10 : $params['size'];
            $skip = ($params['page']-1)*$size;
            $query = $query->skip($skip)->take($size);

            $ret['per_page'] = array_get($params, 'size', 10);
            $ret['current_page'] = array_get($params, 'page', 1);
        }

        $data = $query->get();
        $ret['lists'] = empty($data) ? [] : $data->toArray();

        return $ret;
    }

    /**
     * 创建qb
     * @return mixed
     */
    protected function createQuery()
    {
        return self::newQuery();
    }

    /**
     * @param $searchOptions
     * @return mixed
     */
    public  function getQueryBySearchOptions($searchOptions)
    {
        $qb = self::newQuery();

        if (empty($searchOptions)) {
            return $qb;
        }

        !empty($searchOptions['keywords']) && $qb = $qb->where('keywords', 'like', '%' . $searchOptions['keywords']. '%');
        unset($searchOptions['keywords']);

        foreach ($searchOptions as $key => $value) {
            if (is_array($value)) {
                $qb->whereIn($key, $value);
            } elseif (is_string($value) || is_numeric($value)) {
                $qb->where($key, $value);
            }
        }

        return $qb;
    }
}
